
<template>
  <el-form  ref="dataForm" :rules="rules" :inline="true"  :model="Form">
   
    
<el-form-item label-width="200px" label="甲端一次性费用收款单位" required prop="JIA_1">
                          <input-dialog-a
                            v-model="Form.JIA_1"
                            text="accountUnitName"
                            @getrow="getrowCharge1"
                            @loadata="loadChargeOptions"
                            :config="Charge"
                            :disabled="true"
                          />
                        </el-form-item>


     <el-form-item label-width="200px" label="甲端一次性委托收款单位" >
                          <input-dialog-a
                            v-model="Form.JIA_2"
                            text="accountUnitName"
                            @getrow="getrowCharge2"
                            @loadata="loadChargeOptions"
                            :config="Charge"
                            :disabled="true"
                          />
                        </el-form-item>


      <el-form-item label-width="200px" label="乙端一次性费用收款单位" required prop="YI_1">
                          <input-dialog-a
                            v-model="Form.YI_1"
                            text="accountUnitName"
                            @getrow="getrowCharge3"
                            @loadata="loadChargeOptions"
                            :config="Charge"
                            :disabled="true"
                          />
                        </el-form-item>


     <el-form-item label-width="200px" label="乙端一次性委托收款单位" >
                          <input-dialog-a 
                            v-model="Form.YI_2"
                            text="accountUnitName"
                            @getrow="getrowCharge4"
                            @loadata="loadChargeOptions"
                            :config="Charge"
                            :disabled="true"
                          />
                        </el-form-item>



    
    <el-form-item class="btnDir">
      <el-button @click="onSubmit">保 存</el-button>
      <el-button @click="onCancel">取 消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import requestUtil from '@/utils/requestEnc';
import CustOrderService from "@/api/order/CustOrderService";
import OrderService from "@/api/order/OrderService.js";
import { mapState } from "vuex";
import inputDialogA from "@/components/public/forms/inputDialoga";
import http from '@/utils/http';
export default {
 components: {
		requestUtil,
    CustOrderService,
    OrderService,
    inputDialogA
  },
  computed: {
		...mapState("orders", ["orderItemList"]),
		...mapState("user", ["user"]),
	},
  async mounted(){

   if(this.CTOInfo instanceof Array){
      this.newCTOInfo = this.CTOInfo;
    }else{
      this.newCTOInfo = [this.CTOInfo];
    }
    
    
    
    if(this.newCTOInfo.length > 0){
      var orderItemIds="";
     
      // this.Form.USER_NAME.value = this.user.staffName;
      // this.Form.USER_ID.value = this.user.staffId;
      // this.Form.TASK_OPR_DATE.value = this.$util.dateFormat(new Date(),"yyyy-MM-dd");
 
      for(var i=0;i<this.newCTOInfo.length;i++){
         if(i==0){
           orderItemIds += this.newCTOInfo[i].orderItemId;
         }else{
            orderItemIds += ","+this.newCTOInfo[i].orderItemId;
         }
      }
      var scopeInfos=[]
      scopeInfos.push({scope:"acctItemOrgs"});
      var custSoNumber = this.newCTOInfo[0].custSoNumber;
      var batchNbr =  this.newCTOInfo[0].batchNbr;
      var custId = this.newCTOInfo[0].custId;
      var taskId = this.ODKInfo.taskId;
     
      
      this.orderItemIds = orderItemIds;
     
      const result=await CustOrderService.qryCustomerOrderDetail({custSoNumber,batchNbr,orderItemIds,custId,scopeInfos});
      

      var  acctItemOrgs = result.orderDetails[0]['acctItemOrgs'];
      

      for(var i =0 ;i<acctItemOrgs.length;i++){
        if(acctItemOrgs[i].chargeOrgCd == "100"){
           this.Form.JIA_1 = acctItemOrgs[i].chargeOrgName;
           this.Form.JIA_1_CODE = acctItemOrgs[i].chargeOrgCode;
           this.Form.JIA_2 = acctItemOrgs[i].conChargeOrgName;
           this.Form.JIA_2_CODE = acctItemOrgs[i].conChargeOrgCode;
        }else if(acctItemOrgs[i].chargeOrgCd == "200"){
           this.Form.YI_1 = acctItemOrgs[i].chargeOrgName;
           this.Form.YI_1_CODE = acctItemOrgs[i].chargeOrgCode;
           this.Form.YI_2 = acctItemOrgs[i].conChargeOrgName;
           this.Form.YI_2_CODE = acctItemOrgs[i].conChargeOrgCode;
        }
      }

    }else{
      // this.Form.TASK_NAME.value = "";
    }


  },
  watch:{


  },
  props:{
	 	ODKInfo: {
			type: Object,
			default: () => {
				return {};
			},
		},
		CTOInfo: {
			type: [Array,Object],
			default: () => {
				return [];
			},
		},
  },
  data() {
    return {
    rules:{
    JIA_1:[
        { required: true, message: '甲端一次性费用收款单位不能为空', trigger: 'blur' },
    ],
    YI_1:[
       { required: true, message: '乙端一次性费用收款单位不能为空', trigger: 'blur' },
    ]
 
  },
   newCTOInfo:[],
   Charge: {
				title: "收款单位",
				tableData: [],

				tableForm: {
					items: [
						{
							field: "accountUnitName",
							title: "单位名称",
							itemRender: {
								name: "input",
								attrs: { placeholder: "请输入关键字" },
							},
						},
						{
							itemRender: {
								name: "$button",
								props: { content: "查询", type: "submit", status: "primary" },
							},
						},
					],
					data: {}, //这个data一定要加，否则无法获取查询参数
				},
				tableColumn: [
					{ type: "seq", width: "5%" },
					{
						slots: { default: "accountUnitCode" },
						title: "单位编码",
						width: "25%",
						treeNode: true,
					},
					{ field: "accountUnitName", title: "单位名称", width: "20%" },
					{ field: "unit", title: "所属单位", width: "50%" },
				],
			},
    orderItemIds:"",
    orderTask:{},
    Form:{
      "JIA_1":"",
      "JIA_2":"",
      "YI_1":"",
      "YI_2":"",
      "JIA_1_CODE":"",
      "JIA_2_CODE":"",
      "YI_1_CODE":"",
      "YI_2_CODE":""
    }
     }
  },
  methods: {
    async loadChargeOptions(params) {
      // 
			//获取页面下拉框值


      var param = {
        param: params.accountUnitName == null?"":"accountUnitName:"+params.accountUnitName,
        sqlCode: "findJSDW|accountUnitType:50520001"  
      }

      let res = await http({
        url: `${process.env.VUE_APP_CTX_PPM_QUERY}/ppm-query-server/qryConfigListDefPage`,
        data: param,
        method: "POST",
      });
		if (res.code == 200) {
        this.Charge = res.meta;
        if (this.Charge.dataType == "TREE") {
          //后台返回结构可能更改暂时先这样判断
          if(this.Charge.tableData.length  ==  24){
            let t22 = this.Charge.tableData[22]
            this.Charge.tableData = [t22];
          }else if(this.Charge.tableData.length == 0) {
            this.Charge.tableData = []
          }else {
            this.Charge.tableData = this.Charge.tableData;
          }
        }
        this.submitKey = this.Charge.returnColumn[0]["field"];
        this.echoKey = this.Charge.returnColumn[1]["field"];
      } else {
        
      }

		},
    getRowCustManageHandler(row) {
     
    },
    getrowCharge1(row){
      
      this.Form.JIA_1_CODE = row.accountUnitCode;
    },
    getrowCharge2(row){this.Form.JIA_2_CODE = row.accountUnitCode;},
    getrowCharge3(row){this.Form.YI_1_CODE = row.accountUnitCode;},
    getrowCharge4(row){
      this.Form.YI_2_CODE = row.accountUnitCode;
    },
	 onCancel(){
		
    //  this.$parent.closeDrawer();
   this.$emit('update:show',false)
	},
  async onSubmit() {
     
     
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          
          this.submit();
        } else {
          
          return false;
        }
      });

    },
   async submit(){
       var acctItemOrgs = [];
  for(var i=0;i<this.newCTOInfo.length;i++){
    acctItemOrgs.push({
      actType:"MOD",
      orderItemId:this.newCTOInfo[i].orderItemId,
      chargeOrgCd:"100",
      chargeOrgCode:this.Form.JIA_1_CODE,
      conChargeOrgCode:this.Form.JIA_2_CODE,
      custId:this.newCTOInfo[i].custId
    },{
      actType:"MOD",
      orderItemId:this.newCTOInfo[i].orderItemId,
      chargeOrgCd:"200",
      chargeOrgCode:this.Form.YI_1_CODE,
      conChargeOrgCode:this.Form.YI_2_CODE,
      custId:this.newCTOInfo[i].custId
    });
  }
    
	let msJson = {
    statusCds: this.newCTOInfo[0].statusCd, //订单状态
    custSoNumber: this.newCTOInfo[0].custSoNumber, //流水号  从上个页面带回
    handleOrgId: this.user.staffId,
    custId: this.newCTOInfo[0].custId,
    batchNbr: this.newCTOInfo[0].batchNbr, //批次号
		accNum: "", //设备号,没有不传
		orderItemIds:this.orderItemIds,
		orderAuditId:this.newCTOInfo[0].orderAuditId,
    oneOrmany:"one",
    userId:this.user.staffId,
    orderItemId:this.orderItemIds,
    processInstId:this.newCTOInfo[0].flowInstId,
    acctItemOrgs:acctItemOrgs
      };



		var result = await CustOrderService.chargeUpdate(msJson);
    
   
    if(result.code == 200){
      

         var param = {};
        param.custSoNumber = this.newCTOInfo[0].custSoNumber;
        param.processInstId = this.ODKInfo.processInstId;
        param.activityId = this.ODKInfo.activityInstId;
        param.orderItemId = this.newCTOInfo[0].orderItemId;
        param.userId = this.user.staffId;
        param.oneOrmany = "one";
        param.taskId = "TACHE10560001";
        param.orderAuditId = this.newCTOInfo[0].orderAuditId;
        param.custId = this.newCTOInfo[0].custId;
        param.orderItemIds = this.orderItemIds;
        
        var result = await CustOrderService.doWorkItem(param);
        
        if(result.meta.resultCode == "0"){
              this.$confirm(result.meta.resultMsg, "提示", {
            confirmButtonText: "确定",
                  showCancelButton: false,
                  type: "success",
                  center: true
                  });
        this.$emit("close");
        }else{
          this.$confirm(result.meta.resultMsg, "提示", {
            confirmButtonText: "确定",
                  showCancelButton: false,
                  type: "error",
                  center: true
                  });
        }
    }
    }
  }
}
</script>

<style>
.el-form--inline .el-form-item {
  display: inline-flex;
  margin-right: 0.1rem;
  vertical-align: top;
  width: 42%;
}

.el-form-item__label {
  width: 94px;
}
.btnDir {
  margin-left: 36%;
  margin-top: 19px;
}
</style>
